package com.SDU.policedispatch.service.impl.schedule;

import com.SDU.policedispatch.entity.ScheduleHistory;
import com.SDU.policedispatch.entity.Schedules;
import com.SDU.policedispatch.entity.User;
import com.SDU.policedispatch.mapper.ScheduleHistoryMapper;
import com.SDU.policedispatch.mapper.ScheduleMapper;
import com.SDU.policedispatch.service.schedule.ScheduleService;
import com.SDU.policedispatch.service.utils.UserDetailsImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;


@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private ScheduleHistoryMapper scheduleHistoryMapper;

    @Override
    public List<Schedules> GetSchedules() {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();

        String name = user.getUsername();
        QueryWrapper<Schedules> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_name", name);

        return scheduleMapper.selectList(queryWrapper);
    }

    @Override
    public List<ScheduleHistory> GetHistorySchedule(LocalDate time) {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();

        return scheduleHistoryMapper.getDataByDate(time, user.getUsername());
    }
}
